package com.nny.alarm.alert;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;

import com.nny.alarm.R;
import com.uxwine.cmm.BaseFragment;
import com.uxwine.util.UxToast;

public class NNYFragment extends BaseFragment implements OnClickListener
{

	ImageView[] mivNNY;
	int[] mnImageIds;
	Button mbtnSnooze;
	Animation mAnimation;
	Context mCtx;
	View mView;
	
	static final int TYPE_NAO 	= 0;
	static final int TYPE_NA	= 1;
	static final int TYPE_YANG 	= 2;
	
	OnActionListener mOnActionListener;
	
	public void setOnActionListener(OnActionListener l)
	{
		mOnActionListener = l;
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		mCtx = getActivity();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState)
	{
		mView = inflater.inflate(R.layout.fragment_alert_nny, null);
		mnImageIds = new int[]
		{
			R.drawable.alert_btn_nao,
			R.drawable.alert_btn_na,
			R.drawable.alert_btn_yang
		};
				
		mAnimation = AnimationUtils.loadAnimation(mCtx, R.anim.alert_btn);
		mAnimation.setRepeatCount(2);
		initNNY(getResources(), mCtx.getPackageName());
		return mView;
	}	
	
	protected void updateImages(int type)
	{
		ImageView iv = mivNNY[type];
		if(type == 2)
		{
			if(iv.isSelected())
			{				
				UxToast.sshow(mCtx, "error");							
			}
			else
			{
				for(int i = type - 1; i >= 0; i--)
				{
					ImageView prev = mivNNY[i];
					if(prev.isSelected() && (type - 1) == i)
					{
						action(OnActionListener.ACTION_DISMISS);
						return;
					}
					if(prev.isSelected())
					{
						prev = mivNNY[i + 1];
						prev.startAnimation(mAnimation);
						return;
					}
				}
				mivNNY[0].startAnimation(mAnimation);
			}
		}
		else if(type == 1)
		{
			if(iv.isSelected())
			{
				ImageView next = mivNNY[type + 1];
				if(next.isSelected())
					next.setSelected(false);
				next.startAnimation(mAnimation);
			}
			else
			{
				ImageView prev = mivNNY[type - 1];
				if(prev.isSelected())
				{
					iv.setSelected(true);
					mivNNY[type + 1].startAnimation(mAnimation);
				}
				else
				{
					prev.startAnimation(mAnimation);
				}
			}
		}
		else if(type == 0)
		{
			if(iv.isSelected())
			{
				boolean isSelected = false;
				for(int i = type + 1; i < 3; i++)
				{
					ImageView next = mivNNY[i];
					if(!isSelected && !next.isSelected())
					{
						next.startAnimation(mAnimation);
						isSelected = true;
					}
				}				
			}
			else
			{
				iv.setSelected(true);
				ImageView next = mivNNY[type + 1];
				next.startAnimation(mAnimation);
			}
		}
	}
	
	protected void initNNY(final Resources res, final String defPackage)
	{
		final Random r = new Random(System.currentTimeMillis());
		List<Integer> seeds = new ArrayList<Integer>();
		seeds.add(TYPE_NAO);seeds.add(TYPE_NA);seeds.add(TYPE_YANG);
		
		
		int count = seeds.size();
		int[] types = new int[count];
		for(int m = 0, n = count; m < 3; m++,n--)
		{
			int index = Math.abs(r.nextInt()) % n;
			types[m] = seeds.get(index);
			seeds.remove(index);
		}		
		
		mivNNY = new ImageView[count];
		for(int i = 0; i < count; i++)
		{
			final int id = res.getIdentifier("alert_iv_nny" + i, "id", defPackage);
			ImageView iv = (ImageView)mView.findViewById(id);
			mivNNY[types[i]] = iv;
			initImages(iv, types[i]);
		}
	}
	
	protected void initImages(ImageView iv, int type)
	{
		iv.setImageResource(mnImageIds[type]);
		iv.setClickable(true);
		iv.setOnClickListener(this);
		iv.setTag((Integer)type);
	}
	
	protected boolean action(int type)
	{
		if(null != mOnActionListener)
			return mOnActionListener.onAction(type);
		return false;
	}

	@Override
	public void onClick(View v)
	{
		Object tag = (Object)v.getTag();
		if(null != tag && tag instanceof Integer)
		{
			int index = (Integer)tag;
			updateImages(index);
		}
	}
}
